package net.cyl.controller;

import lombok.extern.slf4j.Slf4j;
import net.cyl.emums.ShortLinkStateEnum;
import net.cyl.service.LogService;
import net.cyl.service.ShortLinkService;
import net.cyl.util.CommonUtil;
import net.cyl.vo.ShortLinkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class LinkApiController {

    @Autowired
    private ShortLinkService shortLinkService;
    @Autowired
    private LogService logService;


    @GetMapping(path = "/{shortLinkCode}")
    public void dispatch(@PathVariable(name = "shortLinkCode") String shortLinkCode,
                         HttpServletRequest servletRequest, HttpServletResponse servletResponse) {

            try {
                if (isLetterDigit(shortLinkCode)) {
                    ShortLinkVO shortLinkVO = shortLinkService.parseShortLinkByCode(shortLinkCode);
                    if(shortLinkVO !=null){
                        logService.recordShortLinkLog(servletRequest,shortLinkCode,shortLinkVO.getAccountNo());
                    }
                    if (isVisitable(shortLinkVO)) {
                        String originalUrl = CommonUtil.removeUrlPrefix(shortLinkVO.getOriginalUrl());
                        servletResponse.setHeader("Location", originalUrl);
                        servletResponse.setStatus(HttpStatus.FOUND.value());
                    } else {
                        servletResponse.setStatus(HttpStatus.NOT_FOUND.value());
                        return;
                    }
                }
//                if(!isLetterDigit(shortLinkCode)){
//                    servletResponse.setStatus(HttpStatus.NOT_FOUND.value());
//                    servletResponse.getWriter().write("短链格式错误");
//                    return;
//                }
//                ShortLinkVO shortLinkVO = shortLinkService.parseShortLinkByCode(shortLinkCode);
//                if(shortLinkVO == null){
//                    servletResponse.setStatus(HttpStatus.NOT_FOUND.value());
//                    servletResponse.getWriter().write("短链不存在");
//                    return;
//                }
//                logService.recordShortLinkLog(servletRequest,shortLinkCode,shortLinkVO.getAccountNo());
//                if(!isVisitable(shortLinkVO)){
//                    servletResponse.setStatus(HttpStatus.NOT_FOUND.value());
//                    servletResponse.getWriter().write("短链已失效");
//                    return;
//                }
//                //直接用完整 URL
//                String originalUrl = shortLinkVO.getOriginalUrl();
//                servletResponse.setStatus(HttpStatus.FOUND.value());
//                servletResponse.setHeader("Location", originalUrl);
//                servletResponse.setHeader("Connection", "close");
            } catch (Exception e) {
                log.error("dispatch error", e);
                servletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
             }
    }




    private static boolean isVisitable(ShortLinkVO shortLinkVO) {
        if(shortLinkVO !=null&& shortLinkVO.getExpired()==null){
            if(ShortLinkStateEnum.AVAILABLE.name().equalsIgnoreCase(shortLinkVO.getState())) {
                return true;
            }
        }
        if(shortLinkVO !=null&& shortLinkVO.getExpired().getTime()> CommonUtil.getCurrentTimestamp()){
            if(ShortLinkStateEnum.AVAILABLE.name().equalsIgnoreCase(shortLinkVO.getState())) {
                return true;
            }
        }
        else if(shortLinkVO !=null&&shortLinkVO.getExpired().getTime() ==-1){
            if(ShortLinkStateEnum.AVAILABLE.name().equalsIgnoreCase(shortLinkVO.getState()))    {
                return true;
            }
        }
        return false;
    }
    private static boolean isLetterDigit(String str) {
        return str.matches("[a-zA-Z0-9]+$");
    }
}
